#!/bin/bash

if [ -z "$(git status --porcelain)" ]; then
    HASH=$(git rev-parse --short HEAD)
    IMAGE=10.1.10.47:5000/crm-web-base:$HASH
    echo "Building base image $IMAGE"
    docker build -f base.dockerfile -t $IMAGE . && \
    sed -i.bak "s/FROM .*/FROM 10.1.10.47:5000\/crm-web-base:$HASH/g" Dockerfile && \
    echo -e "\n" && \
    echo -e "\033[1;32mBuild success, Dockerfile is updated with built image" && \
    echo -e "\033[1;32mYou should do the following:" && \
    echo -e "\033[1;32m    1. push the built image: \033[1;33mdocker push $IMAGE" && \
    echo -e "\033[1;32m    2. commit changes of \033[1;33mDockerfile\033[0m\n"
else
    echo "Git working directory is not clean"
    echo "You should commit your local changes before run this script"
    exit 1
fi